<template>
  <div ref="appContainer" class="app-container home-bg-padding">
    <CustomSearch
      ref="searchForm"
      :show-all.sync="showAll"
      @search="searchFn"
      @reset="resetFn"
      @calHeight="calHeight"
    >
      <template #btns>
        <sec-button type="primary" @click="onAdd">新增</sec-button>
      </template>
      <template #search>
        <sec-input
          v-model="searchForm.keyword"
          class="search-input"
          size="medium"
          placeholder="账号/姓名/手机号/邮箱"
          suffix-icon="sec-icon-sousuo"
          style="width: 300px"
          clearable
        />
      </template>
      <template #select>
        <sec-form-item prop="evtTime" class="search-form-40">
          <sec-date-picker
            v-model="evtTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </sec-date-picker>
        </sec-form-item>
        <sec-form-item prop="roleId">
          <sec-select v-model="searchForm.roleId" placeholder="角色" clearable>
            <sec-option
              v-for="val in constants.roleList"
              :key="val.roleId"
              :label="val.roleName"
              :value="val.roleId"
            ></sec-option>
          </sec-select>
        </sec-form-item>
      </template>
    </CustomSearch>
    <div class="query-table">
      <sec-table
        v-loading="loading.case1"
        :data="tableData"
        size="mini"
        style="width: 100%"
        stripe
        :height="tableHeight"
      >
        <!-- <sec-table-column type="selection" width="50"></sec-table-column> -->
        <sec-table-column label="序号" type="index" width="50">
          <template slot-scope="scope">
            <span>{{
              (pagination.pageNo - 1) * pagination.pageSize + scope.$index + 1
            }}</span>
          </template>
        </sec-table-column>
        <sec-table-column prop="username" label="账号"></sec-table-column>
        <sec-table-column prop="nickName" label="姓名"></sec-table-column>
        <sec-table-column prop="phone" label="手机号"></sec-table-column>
        <sec-table-column prop="email" label="邮箱"></sec-table-column>
        <sec-table-column prop="roleList" label="角色">
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.roleList" :key="index">
              {{ item.roleName }}
            </span>
          </template>
        </sec-table-column>
        <sec-table-column prop="createTime" label="创建时间" width="150">
          <template slot-scope="scope">
            <span>{{
              moment(scope.row.createTime).format('YYYY/MM/DD HH:mm:ss')
            }}</span>
          </template>
        </sec-table-column>
        <sec-table-column fixed="right" label="操作" width="170">
          <template slot-scope="scope">
            <span :style="{ color: '#f1f1f1', padding: '0 5px' }">|</span>
            <sec-button type="text" size="small" @click="onUpdate(scope.row)">
              编辑
            </sec-button>
            <sec-button type="text" size="small" @click="onReset(scope.row)">
              重置密码
            </sec-button>
            <sec-button
              v-show="scope.row.reservedLevel == 1"
              type="text"
              size="small"
              @click="onRemove(scope.row)"
              >删除</sec-button
            >
          </template>
        </sec-table-column>
      </sec-table>
      <div class="pagination">
        <sec-pagination
          :current-page.sync="pagination.pageNo"
          :page-size="pagination.pageSize"
          layout="total,prev, pager, next,sizes"
          :total="pagination.totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></sec-pagination>
      </div>
    </div>
    <ModelForm
      :form-visible="formVisible"
      :constants="constants"
      :row-data="rowData"
      :type="formType"
      @updateList="updateList"
      @closeDetailForm="
        () => {
          formVisible = false
        }
      "
    />
    <ResetModal :reset-visible.sync="resetVisible" />
  </div>
</template>
<script>
import http from '@/utils/http'
import moment from 'moment'
import ModelForm from './components/modelForm.vue'
import pagination from '@/utils/mixins/pagination'
import ResetModal from './components/resetModal.vue'
import CustomSearch from '@/components/CustomSearch'
export default {
  name: 'Report',
  components: { ModelForm, ResetModal, CustomSearch },
  mixins: [pagination],
  data() {
    return {
      constants: {
        NoticeLevel: {
          10: '通知',
          20: '告警'
        },
        roleList: []
      },
      searchForm: {},
      tableData: [],
      pagination: {
        pageNo: 1,
        pageSize: 10,
        totalCount: 0
      },
      rowData: {},
      formVisible: false,
      resetVisible: false,
      formType: 'add',
      evtTime: [],
      showAll: true
    }
  },
  watch: {
    evtTime(val) {
      const [startTime, endTime] = val || []
      this.searchForm.startTime = startTime
        ? moment(startTime).format('YYYY-MM-DD HH:mm:ss')
        : ''
      this.searchForm.endTime = endTime
        ? moment(endTime).format('YYYY-MM-DD HH:mm:ss')
        : ''
    }
  },
  created() {
    this.moment = moment
    this.getRoleList()
  },
  mounted() {
    this.getList()
  },
  methods: {
    getRoleList() {
      http.roleList().then(({ data }) => {
        this.constants.roleList = data || []
      })
    },
    getList(
      params = {
        pageNo: 1,
        pageSize: this.pagination.pageSize,
        _updatedAt: Date.now()
      }
    ) {
      this.loadingControl(['case1'], true)
      http
        .accountList(params)
        .then(({ data }) => {
          this.loadingControl(['case1'], false)
          const { rows, totalRows, pageNo, pageSize } = data
          this.tableData = rows
          this.pagination = {
            ...this.pagination,
            pageNo,
            pageSize,
            totalCount: totalRows
          }
        })
        .catch(() => {
          this.loadingControl(['case1'], false)
        })
    },
    onView(row) {
      this.formType = 'view'
      this.rowData = row
      this.formVisible = true
    },
    onUpdate(row) {
      this.rowData = row
      this.formType = 'update'
      this.formVisible = true
    },
    onReset(row) {
      this.rowData = row
      this.resetVisible = true
    },
    onAdd() {
      this.formType = 'add'
      this.formVisible = true
    },
    onRemove(row) {
      this.$confirm('确认删除所选账号?').then(() => {
        return http
          .accountDelete(row)
          .then((res) => {
            if (res.code === '200') {
              this.$message.success('删除成功')
            }
            this.updateList()
          })
          .catch(() => {})
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.ellipsis {
  width: 280px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  white-space: pre-wrap;
  word-break: break-all;
  word-wrap: normal;
}
.table-button {
  margin-bottom: 8px;
}
</style>
<style lang="scss"></style>
